package com.xhh.fs.service;

import com.xhh.fs.dao.BookDAO;
import com.xhh.fs.pojo.Book;
import com.xhh.fs.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    CategoryService categoryService;
    @Autowired
    BookDAO bookDAO;
    //查出全部书籍
    public List<Book> findAll(){
        Sort sort = Sort.by(Sort.DEFAULT_DIRECTION, "id");
        return bookDAO.findAll(sort);
    }
    //增加或新增书籍
    public void ajax(Book book){
        bookDAO.save(book);
    }
    //通过ID删除书籍
    public void deleteById(int id){
        bookDAO.deleteById(id);
    }
    //搜索栏查询(模糊查询)
    public List<Book> search(String keywords){
        return bookDAO.findAllByTitleLikeOrAuthorLike('%' + keywords + '%','%' + keywords + '%');
    }
    //通过分类查询书籍
    public List<Book> findByCategory(int cid){
        Category category = categoryService.get(cid);
        return bookDAO.findAllByCategory(category);
    }
}
